﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using BrainTechLLC;

namespace BrainTechLLC.FilterExtensions
{
    public partial class FilterCollectionCollection
    {
        private static string GetFileNameFromCollectionName(string collectionName)
        {
            return "C:\\" + collectionName + ".filter";
        }

        public static FilterCollectionCollection Load(string path)
        {
            FilterCollectionCollection result = new FilterCollectionCollection();
            bool atLeastOne = false;

            if (File.Exists(path))
            {
                try
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    List<string> filterCollectionNames = bytes.DeserializeFromCompressedBinary(true) as List<string>;
                    if (filterCollectionNames != null)
                    {
                        for (int n = 0; n < filterCollectionNames.Count; n++)
                        {
                            var collectionName = filterCollectionNames[n];
                            FilterCollection collection = null;

                            try
                            {
                                collection = FilterCollection.Load(GetFileNameFromCollectionName(collectionName));
                            }
                            catch (Exception exInner)
                            {
                                Console.WriteLine(exInner.ToString());
                            }

                            if (collection != null)
                            {
                                atLeastOne = true;
                                result.AddFilterCollection(collection);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            if (!atLeastOne)
            {
                try
                {
                    var collection = FilterCollection.Load(@"C:\Filters.dat");

                    if (collection != null)
                    {
                        result.AddFilterCollection(collection);
                    }
                }
                catch
                {
                    FilterCollection fc = new FilterCollection();
                    fc.CollectionName = "Test";
                    result.AddFilterCollection(fc);
                }
            }

            return result;
        }

        public void Save(string path)
        {
            var results = new List<string>();

            foreach (var collection in this)
            {
                results.Add(collection.CollectionName);
                collection.Save(GetFileNameFromCollectionName(collection.CollectionName));
            }

            byte[] bytes = new byte[0];
            try
            {
                bytes = results.SerializeToCompressedBinary(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                Debug.Assert(false);
            }
            File.WriteAllBytes(path, bytes);
        }        
    }
}
